package com.etc.controler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.entity.User;
import com.etc.service.UserService;
import com.etc.service.impl.UserServiceImpl;
import com.etc.util.Commen;
import com.google.gson.Gson;

/**
 * Servlet implementation class UserDelServlet
 */
@WebServlet("/user/up")
public class UserUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private UserService us = new UserServiceImpl();
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    //设置请求编码
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		//设置json格式
		response.setContentType("application/json");

		StringBuffer myJson = new StringBuffer();
		
		//得到一个BufferedReader对象
		BufferedReader reader = request.getReader();
		
		String line = null;
		while((line=reader.readLine())!=null) {
			myJson.append(line);
		}
		
		Gson gson = new Gson();
		//将json字符串转化为java中的对象
		User user = gson.fromJson(myJson.toString(), User.class);
        System.out.println("user:"+user);
        
        User u = new User(user.getuName(),user.getuPwd(),user.getuSex(),user.getuState());
        boolean flag = us.updateUser(u);
		Commen cm = new Commen(flag?"修改成功":"修改失败");
		
		String str = gson.toJson(cm);
	
		PrintWriter out = response.getWriter();
		out.print(str);
		out.close();
		
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}
}
